package com.hby.game

import android.graphics.Bitmap
import android.os.Debug
import android.os.Handler
import android.os.Looper
import android.util.Log
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.R
import com.helloandroid.tools.AppConfig
import com.helloandroid.tools.BitmapMemCache
import game.engine.base.GameActivity

class HbyGame : GameActivity() {

    class Res {
        lateinit var bgBitmap: Bitmap
        lateinit var infoBgBitmap: Bitmap
        lateinit var wordBitmap1: Bitmap
        lateinit var wordBitmap2: Bitmap
        lateinit var wordBitmap3: Bitmap
        lateinit var titleBitmap: Bitmap
        lateinit var coinBitmap: Bitmap
        lateinit var timeBitmap: Bitmap
        lateinit var hbBitmap1: Bitmap
        lateinit var hbBitmap2: Bitmap

        fun loadRes() {
            bgBitmap = BitmapMemCache.initBitmap(R.drawable.hby_bg)
            infoBgBitmap = BitmapMemCache.initBitmap(R.drawable.hby_info_bg)
            wordBitmap1 = BitmapMemCache.initBitmap(R.drawable.hby_num1)
            wordBitmap2 = BitmapMemCache.initBitmap(R.drawable.hby_num2)
            wordBitmap3 = BitmapMemCache.initBitmap(R.drawable.hby_num3)
            titleBitmap = BitmapMemCache.initBitmap(R.drawable.hby_title)
            coinBitmap = BitmapMemCache.initBitmap(R.drawable.hby_small_coin)
            timeBitmap = BitmapMemCache.initBitmap(R.drawable.hby_time_icon)
            hbBitmap1 = BitmapMemCache.initBitmap(R.drawable.hby_hb1)
            hbBitmap2 = BitmapMemCache.initBitmap(R.drawable.hby_hb2)
        }

        fun releaseRes() {
//        bgBitmap.recycle()
//        infoBgBitmap.recycle()
//        wordBitmap1.recycle()
//        wordBitmap2.recycle()
//        wordBitmap3.recycle()
//        titleBitmap.recycle()
//        coinBitmap.recycle()
//        timeBitmap.recycle()
//        hbBitmap1.recycle()
//        hbBitmap2.recycle()
        }
    }

    companion object {
        val res = Res()
    }

    val handler = Handler(Looper.myLooper()!!)
    var pressBackTimeMs: Long = 0L

    override fun launchGame() {
        res.loadRes()
        director.startScene(ReadyScene())

        if (AppConfig.isDebug) {
            Log.e("Mem", "========= 红包雨=显示内存=========")
            Log.e("Mem", "getNativeHeapAllocatedSize mem:" + Debug.getNativeHeapAllocatedSize() / 1024 / 1024 + "M")
            Log.e("Mem", "getNativeHeapFreeSize mem:" + Debug.getNativeHeapFreeSize() / 1024 / 1024 + "M")
            Log.e("Mem", "getNativeHeapSize mem:" + Debug.getNativeHeapSize() / 1024 / 1024 + "M")
        }
    }

    override fun showResult(coins: Int) {
        handler.post {
            if (coins > 0) {
                HbyExitDialog(this, coins).show()
            } else {
                HbyEmptyDialog(this).show()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        res.releaseRes()
    }

    override fun isDarkMode(): Boolean = false

    override fun onBackPressed() {
        if (System.currentTimeMillis() - pressBackTimeMs >= 2000L) {
            AppUtil.toast("再按一次退出!")
            pressBackTimeMs = System.currentTimeMillis()
        } else {
            super.onBackPressed()
        }
    }
}